from django.core.paginator import Paginator
from django.shortcuts import render
from django.shortcuts import redirect
from django.urls import  reverse
from django.http import HttpResponse
from myadmin.models import User,Shop,Category,Product
from datetime import datetime
from myadmin.models import Orders,Payment,OrdersDetail,User,Member

def index(request,pagIndex=1):
    umod =Orders.objects
    sid = request.session['shopinfo']['-id']
    ulist = umod.filter(shop_id=sid)
    mywhere = []

    #获取判断并封装status的搜索条件
    status = request.GET.get('status','')
    if status != '':
        ulist = ulist.filter(status=status)
        mywhere.append('status='+status)

    #执行分页处理
    pagIndex = int(pagIndex)
    page = Paginator(ulist,10)
    maxpages = page.num_pages

    #判断当前分页是否越界
    if pagIndex > maxpages:
        pagIndex = maxpages
    if pagIndex < 1:
        pagIndex = 1

    list2 = page.page(pagIndex) #获取当前页数据
    plist = page.page_range #读取页码列表信息


    for vo in list2:
        if vo.user_id == 0:
            vo.nickname = '无'
        else:
            user = User.objects.only('nickname').get(id=vo.user_id)
            vo.nickname = user.nickname

        if vo.member_id == 0:
            vo.membername = '大堂顾客'
        else:
            member = Member.objects.only('mobile').get(id=vo.member_id)
            vo.membername = member.mobile

    context ={'orderslist':list2,'plist':plist,'pagIndex':pagIndex,'maxpages':maxpages,'mywhere':mywhere}
    return render(request,'web/list.html',context)

def insert(request):
    try:
        od = Orders()
        od.shop_id = request.session['shopinfo']['id']
        od.member_id = 0
        od.user_id = request.session['webuser']['id']
        od.money = request.session['total_money']

        od.payment_status = 2
        od.status = 1
        od.create_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        od.update_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        od.save()

        op = Payment()
        op.order_id = od.id
        op.member_id = 0
        op.type = 2
        op.bank = request.GET.get('bank',3)
        op.money = request.session['total_money']
        op.status = 2
        op.create_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        op.update_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        op.save()

        cartlist =request.session.get('cartlist',{})
        for item in cartlist.values():
            ov = OrdersDetail()
            ov.order_id = od.id
            ov.product_id = item['id']
            ov.product_name = item['name']
            ov.price = item['price']
            ov.quantity = item['num']
            ov.status = 1
            ov.save()
        del request.session['cartlist']
        del request.session['total_money']
        return HttpResponse('Y')

    except Exception as err :
        print(err)
        return HttpResponse('N')


def detail(request):
    oid = request.GET.get('oid',0)
    dlist = OrdersDetail.objects.filter(order_id=oid)
    context = {'detaillist':dlist}
    return render(request,'web/detail.html',context)

def status(request):
    try:
        oid = request.GET.get('oid',0)
        ob = Orders.objects.get(id=oid)
        ob.status = request.GET['status']
        ob.save()
        return HttpResponse('Y')
    except Exception as err:
        print(err)
        return HttpResponse('N')
